package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient vodUploadClient;








    /**
     * 删除音频文件
     *
     * @param mediaFileId
     */
    @Override
    public void deleteTrackMedia(String mediaFileId) {

        try {
            //1.实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            //2.实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);
            //3.实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            //4.返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            client.DeleteMedia(req);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }



    /**
     * 将音频文件上传到腾讯云点播平台
     *
     * @param file 音频文件
     * @return {mediaFileId:"文件唯一标识",mediaUrl:"播放地址"}
     */
    @SneakyThrows
    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        //现将声音数据上传到本地的暂存目录下
        String tempFilePath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
        //创建vodclient上传声音数据所需的条件参数  条件参数需要拿到声音数据暂存的目录
        VodUploadRequest vodUploadRequest = new VodUploadRequest(tempFilePath);
        //上传声音数据到云点播
        VodUploadResponse upload = vodUploadClient.upload(vodConstantProperties.getRegion(), vodUploadRequest);
        //判断上传是否成功
        if(upload!=null){
            Map<String,String> mapResult = new HashMap<>();
            mapResult.put("mediaFileId", upload.getFileId());
                mapResult.put("mediaUrl", upload.getMediaUrl());
                return mapResult;
        }
        return null;

//        try {
//            //1.将上传文件保存到临时上传目录下
//            //UploadFileUtil.uploadTempPath() 方法接收两个参数：第一个参数是临时路径的根目录，第二个参数是要上传的文件对象。该方法会将文件保存到指定的临时路径中，并返回该文件在临时路径中的完整路径。
//
//            String tempFilePath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
//
//            //1.TODO 已有该对象注入即可 获取到上传客户端对象VodUploadClient
//
//            //2.构造上传请求对象VodUploadRequest
//            VodUploadRequest request = new VodUploadRequest();
//            request.setMediaFilePath(tempFilePath);
//            //3.调用上传
//            VodUploadResponse uploadResponse = vodUploadClient.upload(vodConstantProperties.getRegion(), request);
//            //4.封装响应结果Map
//            if (uploadResponse != null) {
//                Map<String, String> mapResult = new HashMap<>();
//                mapResult.put("mediaFileId", uploadResponse.getFileId());
//                mapResult.put("mediaUrl", uploadResponse.getMediaUrl());
//                return mapResult;
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return null;
    }

    /**
     * 根据云点播平台文件唯一标识，获取音频文件详情信息
     *
     * @param mediaFileId 文件唯一标识
     * @return
     */
    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        try {
            //1.实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            //2.实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            //3.实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            //3.1 封装获取详情对应文件唯一标识
            req.setFileIds(fileIds1);

            //4.调用获取文件详情接口。返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse describeMediaInfosResponse = client.DescribeMediaInfos(req);
            //5.解析结果对象获取音频文件时长，大小，类型
            if (describeMediaInfosResponse != null) {
                MediaInfo[] mediaInfoSet = describeMediaInfosResponse.getMediaInfoSet();
                if (mediaInfoSet != null && mediaInfoSet.length > 0) {
                    TrackMediaInfoVo vo = new TrackMediaInfoVo();
                    MediaInfo mediaInfo = mediaInfoSet[0];
                    //5.1 获取媒体文件基本信息对象
                    MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
                    vo.setType(basicInfo.getType());

                    //5.2 获取媒体文件元信息对象
                    MediaMetaData metaData = mediaInfo.getMetaData();
                    vo.setDuration(metaData.getAudioDuration());
                    vo.setSize(metaData.getSize());
                    return vo;
                }
            }
        } catch (Exception e) {
            throw new GuiguException(400, "音频文件详情获取异常！");
        }
        return null;
    }

}
